package com.hishop.payment.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alipay.api.AlipayApiException;
import com.hishop.payment.service.PayInfoService;
import com.hishop.pojo.User;

/**
 * @author
 * @version 创建时间：2017年10月10日 下午1:28:09
 */
@Controller
public class OrderController{
	final static Logger log = LoggerFactory.getLogger(OrderController.class);
	@Autowired
	private PayInfoService payInfoService;
	private String paramList;
	
	/**
	 * 生成预支付订单信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/order/getPreparePayInfo", method = { RequestMethod.POST})	
	public @ResponseBody String getPreparePayInfo(HttpServletRequest request,HttpServletResponse response) {
		String studentId = request.getParameter("student_id");
		String packageId = request.getParameter("package_id");
		String name = request.getParameter("name");
		String description = request.getParameter("description");
		String price = request.getParameter("price");
		String recommendCode = request.getParameter("recommend_code");
		String months = request.getParameter("months");
		String result = payInfoService.getPreparePayInfo(studentId, packageId,months,recommendCode,price,name,description);
		return result;
	}

	/**
	 * 查询支付结果
	 * @param request
	 * @param student_id
	 * @param order_id
	 * @return
	 */
	@RequestMapping(value = "/order/getPayStatus", method = { RequestMethod.POST})
	@ResponseBody
	public String getPayStatus(HttpServletRequest request) {
		String orderid = request.getParameter("orderid");
		String result = payInfoService.getPayStatus(orderid);
		return result;
	}
	
	/**
	 * 查询订单列表信息（暂不用）
	 * @param request
	 * @param student_id
	 * @param order_id
	 * @return
	 */
	@RequestMapping(value = "/order/getOrderList", method = { RequestMethod.POST})
	@ResponseBody
	public String getOrderList(HttpServletRequest request,String student_id,
			@RequestParam(value = "order_id", defaultValue = "") String order_id,
			@RequestParam(value = "page_size", defaultValue = "10") int page_size,
			@RequestParam(value = "page", defaultValue = "1") int page) {
		String result = payInfoService.getOrderList(student_id, order_id,page_size,page);
		return result;
	}

	/**
	 * 支付宝回调订单结果
	 * 1.商户确定该笔交易付款是否成功需要依赖服务端收到支付宝异步通知的结果进行判断   
	 * 2.异步通知验签
	 * 3.避免重复通知
	 * 4.注意：不要修改接口，不要加请求方法，出现了编码问题，会导致验签失败
	 * @param request
	 * @return
	 * @throws AlipayApiException 
	 */
	@RequestMapping(value = "/order/alipay_callback")
	@ResponseBody
	public String alipayCallback(HttpServletRequest request) {
	    return payInfoService.aliCallback(request.getParameterMap());
	}
}
